import com.alibaba.fastjson.JSON;
import com.qian.api.ddj.util.SignUtils;
import com.yaowk.api.utils.HttpUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

public class PayTest02 {

    String UserId = "10018";
    String Key = "xtybifpqhml706eozxrtky688qyq3pn8";

    @Test
    public void pay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, String> map = new TreeMap<>();
        map.put("pay_memberid", UserId);
        map.put("pay_orderid", System.currentTimeMillis() + "");
        map.put("pay_amount", "0");
        map.put("pay_applydate", format.format(new Date()));
//        map.put("pay_applydate", "2017-12-19 10:43:49");
        map.put("pay_bankcode", "902");
        map.put("pay_notifyurl", "www.baidu.com");
        map.put("pay_callbackurl", "www.baidu.com");

        System.out.println(SignUtils.sign(map, Key));

        map.put("pay_md5sign", SignUtils.sign(map, Key).toUpperCase());
        map.put("pay_productname", "123456");
        System.out.println("请求数据" + SignUtils.params(map));
        String receive = HttpUtils.post("http://u9pay.com/Pay_Index.html", SignUtils.params(map));
        System.out.println(receive);

        Document document = Jsoup.parse(receive);
        Elements elementsByTag = document.getElementById("weixin").getElementsByTag("img");
        String qrcode = "http://u9pay.com" + elementsByTag.get(1).attr("src");
        System.out.println(qrcode);
    }

    @Test
    public void pay2() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, String> map = new TreeMap<>();
        map.put("pay_memberid", UserId);
        map.put("pay_orderid", System.currentTimeMillis() + "");
        map.put("pay_amount", "2");
        map.put("pay_applydate", format.format(new Date()));
//        map.put("pay_applydate", "2017-12-19 10:43:49");
        map.put("pay_bankcode", "904");
        map.put("pay_notifyurl", "www.baidu.com");
        map.put("pay_callbackurl", "www.baidu.com");

        System.out.println(SignUtils.sign(map, Key));

        map.put("pay_md5sign", SignUtils.sign(map, Key).toUpperCase());
        map.put("pay_productname", "123456");
        System.out.println("请求数据" + SignUtils.params(map));
        String receive = HttpUtils.post("http://u9pay.com/Pay_Index.html", SignUtils.params(map));
        System.out.println(receive);

    }

    @Test
    public void wxwap() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, String> map = new TreeMap<>();
        map.put("pay_memberid", UserId);
        map.put("pay_orderid", System.currentTimeMillis() + "");
        map.put("pay_amount", "1.18");
        map.put("pay_applydate", format.format(new Date()));
//        map.put("pay_applydate", "2017-12-19 10:43:49");
        map.put("pay_bankcode", "901");
        map.put("pay_notifyurl", "www.baidu.com");
        map.put("pay_callbackurl", "www.baidu.com");

        System.out.println(SignUtils.sign(map, Key));

        map.put("pay_md5sign", SignUtils.sign(map, Key).toUpperCase());
        map.put("pay_productname", "123456");
        System.out.println("请求数据" + SignUtils.params(map));
        HttpURLConnection conn = null;
        String receive = null;
        try {
            URL _url = new URL("http://u9pay.com/Pay_Index.html");
            conn = (HttpURLConnection) _url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setConnectTimeout(50000);
            conn.setReadTimeout(50000);
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36");
            conn.connect();
            OutputStream out = conn.getOutputStream();
            out.write(HttpUtils.formatData(map).getBytes("UTF-8"));
            out.flush();
            out.close();

            System.out.println(JSON.toJSONString(conn.getHeaderFields()));
            String location = conn.getHeaderField("Location");
            System.out.println(location);
            System.out.println(conn.getURL().toString());
            receive = HttpUtils.get(location);

        } catch (Exception var9) {
            throw new RuntimeException(var9);
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }

        System.out.println(receive);
    }

    @Test
    public void zfbwap() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, String> map = new TreeMap<>();
        map.put("pay_memberid", UserId);
        map.put("pay_orderid", System.currentTimeMillis() + "");
        map.put("pay_amount", "1.18");
        map.put("pay_applydate", format.format(new Date()));
//        map.put("pay_applydate", "2017-12-19 10:43:49");
        map.put("pay_bankcode", "904");
        map.put("pay_notifyurl", "www.baidu.com");
        map.put("pay_callbackurl", "www.baidu.com");

        System.out.println(SignUtils.sign(map, Key));

        map.put("pay_md5sign", SignUtils.sign(map, Key).toUpperCase());
        map.put("pay_productname", "123456");
        System.out.println("请求数据" + SignUtils.params(map));
        HttpURLConnection conn = null;
        String receive = null;
        try {
            URL _url = new URL("http://u9pay.com/Pay_Index.html");
            conn = (HttpURLConnection) _url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setConnectTimeout(50000);
            conn.setReadTimeout(50000);
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36");
            conn.connect();
            OutputStream out = conn.getOutputStream();
            out.write(HttpUtils.formatData(map).getBytes("UTF-8"));
            out.flush();
            out.close();

            System.out.println(JSON.toJSONString(conn.getHeaderFields()));
            String location = conn.getHeaderField("Location");
            System.out.println(location);
            System.out.println(conn.getURL().toString());

            String url = conn.getURL().toString();
            System.out.println(url.replaceFirst("http://a.bldpay.com:8209/payapi/wap/\\?url=", ""));
//            receive = HttpUtils.get(location);

        } catch (Exception var9) {
            throw new RuntimeException(var9);
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }

        System.out.println(receive);
    }
}
